package com.cardbaobao.cardbabyclient.utils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 完成对json数据的解析
 *
 * @author zad
 */
public class JsonUtil {

    /**
     * 获取最外层结果
     * @param jsonString
     * @return JavaBean对象
     * @描述 获取 JavaBean对象
     * @数据格式 {"code":1,"result":"success","msg":"保存成功"}
     */
    public static <T> T getResult(String jsonString, Class<T> clazz) {
        if (TextUtils.isEmpty(jsonString)) {
            return null;
        }
        T t = null;
        try {
            t = JSON.parseObject(jsonString, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * @param jsonString
     * @return JavaBean对象
     * @描述 获取 JavaBean对象
     * @数据格式 "result":"success","code":"","data":{}
     */
    public static <T> T getJavaBean(String jsonString, Class<T> clazz) {
        if (TextUtils.isEmpty(jsonString)) {
            return null;
        }
        T t = null;
        try {
            JSONObject jObject = JSON.parseObject(jsonString);
            JSONObject jsnObject = jObject.getJSONObject("data");
            t = JSON.parseObject(jsnObject.toJSONString(), clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * @param jsonString
     * @return JavaBean对象
     * @描述 获取 JavaBean对象
     * @数据格式 "result":"success","code":"","msg":{"data":{}}
     */
    public static <T> T getData(String jsonString, Class<T> clazz) {
        if (TextUtils.isEmpty(jsonString)) {
            return null;
        }
        T t = null;
        try {
            JSONObject jObject = JSON.parseObject(jsonString);
            JSONObject msgObject = jObject.getJSONObject("msg");
            JSONObject dataObject = msgObject.getJSONObject("data");
            t = JSON.parseObject(dataObject.toJSONString(), clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * @param jsonString
     * @return List<T> 映射后的对象列表
     * @数据格式 "result":"success","code":"","msg":{"data":[]}
     */
    public static <T> List<T> getDataListsByMsg(String jsonString, Class<T> clazz) {
        if (TextUtils.isEmpty(jsonString)) {
            return null;
        }
        List<T> list = null;
        try {
            JSONObject jObject = JSON.parseObject(jsonString);
            JSONObject msgObject = jObject.getJSONObject("msg");
            JSONArray dataArray = msgObject.getJSONArray("data");
            list = JSON.parseArray(dataArray.toJSONString(), clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * @param jsonString
     * @param clazz
     * @return List<T> 映射后的对象列表
     * @数据格式 "result":"success","code":"","data":[{},{}]
     */
    public static <T> List<T> getDataLists(String jsonString, Class<T> clazz) {
        if (TextUtils.isEmpty(jsonString)) {
            return null;
        }
        List<T> list = new ArrayList<>();
            try {
                JSONObject jObject = JSON.parseObject(jsonString);

                JSONArray jArray = jObject.getJSONArray("data");
                // 通过FastJSON对数据的解析
                list = JSON.parseArray(jArray.toJSONString(), clazz);
            } catch (Exception e) {
                e.printStackTrace();
        }
        return list;
    }

    public static <T> T json2Object(String json, Class<T> tClass) {
        return JSON.parseObject(json, tClass);
    }

    public static String object2Json(Object object) {
        return JSON.toJSONString(object);
    }

}